package com.yaorange.controller;

import com.github.pagehelper.PageInfo;
import com.yaorange.api.vo.Result;
import com.yaorange.entity.User;
import com.yaorange.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 用户控制器
 * @company: yaorange
 * @author: CorJ
 * @version: 1.0
 * @create: 2021-11-28 14:34
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<?> queryPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize){

        try {
            PageInfo<User> pageInfo = userService.pageUser(pageNum,pageSize);
           /* result.setSuccess(true);
            result.setResult(pageInfo);
            result.setCode(CommonConstant.SC_OK_200);
            result.setMessage("查询分页成功");
            result.setTimestamp(System.currentTimeMillis());*/
            return Result.ok(pageInfo);
        }catch (Exception e){
           /* result.setSuccess(false);
            result.setResult(null);
            result.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
            result.setMessage("查询分页失败");
            result.setTimestamp(System.currentTimeMillis());*/
            return Result.error("error");
        }
    }
    @GetMapping("/page")
    public Result<?> queryPage1(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){

        try {
            PageInfo<User> pageInfo = userService.pageUser(pageNum,pageSize);
           /* result.setSuccess(true);
            result.setResult(pageInfo);
            result.setCode(CommonConstant.SC_OK_200);
            result.setMessage("查询分页成功");
            result.setTimestamp(System.currentTimeMillis());*/
            return Result.ok(pageInfo);
        }catch (Exception e){
         /*   result.setSuccess(false);
            result.setResult(null);
            result.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
            result.setMessage("查询分页失败");
            result.setTimestamp(System.currentTimeMillis());*/
            return Result.error("error");
        }
    }
    @PostMapping("/add")
    public String add(@RequestBody User user){
      /*  Result<Object> result = new Result<>();
        if(userService.addUser(user)){
            result.setMessage("添加成功");
            return result;
        }
        result.setMessage("添加失败");
        return result;*/
        boolean rs = userService.addUser(user);
        if (rs){
            return "添加成功";
        }
        return "添加失败";
    }
    @PostMapping("/login")
    public Result<?> login(@RequestBody User loginUser){
        User user = userService.getUserByName(loginUser.getLoginName());
        if(user!=null){
            if(user.getPassword().equals(loginUser.getPassword())){
                return Result.ok(user,"登录成功");
            }
        }
        return Result.error("用户名或密码错误");
    }
    @PostMapping("/{id}")
    public Result<?> getUser(@PathVariable Integer id){
       User user = userService.getUserById(id);
        return Result.ok(user);
    }
}
